import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:gluz/app/common/common_server.dart';
import 'package:gluz/app/data/dictionary_entity.dart';
import 'package:gluz/app/utils/colors.dart';

import '../../app_session.dart';
import '../data/area_entity.dart';
import '../data/country_entity.dart';
import 'common_button.dart';

class BottomModalSheet {
  static void showCountryList(void Function(CountryEntity entity) onFinished) {
    showModalBottomSheet(
      context: Get.context!,
      builder: (ctx) {
        return Container(
          height: 0.5.sh,
          decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.only(
              topLeft: const Radius.circular(10.0),
              topRight: const Radius.circular(10.0),
            ),
          ),
          child: Column(
            children: [
              Padding(
                padding: const EdgeInsets.all(15.0),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Text('取消', style: TextStyle(color: Colors.white)),
                    Text(
                      '选择国家',
                      style: TextStyle(
                        fontSize: 16,
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                    InkWell(onTap: Get.back, child: Text('取消')),
                  ],
                ),
              ),
              Expanded(
                child: ListView.builder(
                  itemExtent: 40,
                  itemCount: CountryEntity.countryList.length,
                  itemBuilder: (ctx, index) {
                    final entity = CountryEntity.countryList[index];
                    return ListTile(
                      leading: SizedBox(
                        width: 20,
                        height: 13,
                        child: Image.asset(entity.image),
                      ),
                      title: Text(entity.name, style: TextStyle(fontSize: 14)),
                      trailing: Text('+${entity.code}'),
                      onTap: () {
                        onFinished(entity);
                        Get.back();
                      },
                    );
                  },
                ),
              ),
            ],
          ),
        );
      },
    );
  }

  static void showAreaSector(void Function(AreaEntity) callback) {
    final controller = Get.put(CommonServerController());
    showModalBottomSheet(
      context: Get.context!,
      isScrollControlled: true,
      builder: (ctx) {
        controller.selectedCity = null;
        controller.selectedCountry = null;
        controller.selectedProvince = null;
        controller.areaList = [];
        controller.loadCountryList(0);
        return GetBuilder<CommonServerController>(
          init: controller,
          builder: (controller) {
            return Container(
              height: 0.7.sh,
              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.only(
                  topLeft: const Radius.circular(10.0),
                  topRight: const Radius.circular(10.0),
                ),
              ),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      IconButton(
                        onPressed: Get.back,
                        icon: Icon(Icons.close, color: Colors.white),
                      ),
                      Text(
                        '城市选择',
                        style: TextStyle(
                          fontSize: 16,
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                      IconButton(onPressed: Get.back, icon: Icon(Icons.close)),
                    ],
                  ),

                  // 搜索框待实现
                  

                  if (controller.historyAreaList.isNotEmpty)
                    ...[
                      Row(
                        children: [
                          const SizedBox(width: 15),
                          Text(
                            '历史选择',
                            style: TextStyle(fontWeight: FontWeight.bold),
                          ),
                        ],
                      ),

                      Padding(
                        padding: const EdgeInsets.symmetric(horizontal: 15, vertical: 10),
                        child: Wrap(
                          spacing: 5,
                          runSpacing: 5,
                          children: controller.historyAreaList.map((e) {
                            return InkWell(
                              onTap: () {
                                callback(e);
                                Get.back();
                              },
                              child: Container(
                                decoration: BoxDecoration(
                                  color: Colors.grey.withAlpha(40),
                                  borderRadius: BorderRadius.circular(4),
                                ),
                                padding: EdgeInsets.symmetric(horizontal: 5, vertical: 3),
                                child:  Text(e.mergename, style: TextStyle(color: Colors.grey, fontSize: 10),),
                              ),
                            );

                        }).toList(),),
                      ),
                    ],

                  if (controller.isSelectedCountry)
                    ...[
                      Row(
                        children: [
                          const SizedBox(width: 15),
                          Text(
                            '当前选择',
                            style: TextStyle(fontWeight: FontWeight.bold),
                          ),
                        ],
                      ),

                      Row(
                        children: [
                          const SizedBox(width: 15),
                          OutlinedButton(
                            onPressed: () {
                              controller.selectedProvince = null;
                              controller.loadCountryList(controller.selectedCountry?.pid ?? 0);
                            },
                            style: OutlinedButton.styleFrom(
                              padding: EdgeInsets.symmetric(horizontal: 5, vertical: 3),
                              minimumSize: Size(10, 10),
                              shape: RoundedRectangleBorder(
                                borderRadius: BorderRadius.circular(5.0),
                              ),
                              side: BorderSide(width: 1, color: Colors.orange),
                            ),
                            child: Text(
                              controller.selectedCountry?.shortname ?? '',
                              style: TextStyle(fontSize: 12, color: Colors.orange),
                            ),
                          ),
                          if (controller.selectedProvince != null)
                            ...[
                              Text(' - ', style: TextStyle(color: Colors.orange),),
                              OutlinedButton(
                                onPressed: () {
                                  controller.loadCountryList(controller.selectedProvince?.pid ?? 0);
                                },
                                style: OutlinedButton.styleFrom(
                                  padding: EdgeInsets.symmetric(horizontal: 5, vertical: 3),
                                  minimumSize: Size(10, 10),
                                  shape: RoundedRectangleBorder(
                                    borderRadius: BorderRadius.circular(5.0),
                                  ),
                                  side: BorderSide(width: 1, color: Colors.orange),
                                ),
                                child: Text(controller.selectedProvince?.shortname ?? '', style: TextStyle(fontSize: 12, color: Colors.orange),),
                              ),
                            ],

                        ],
                      ),
                    ],


                  Row(
                    children: [
                      const SizedBox(width: 15),
                      Text(
                        '请选择',
                        style: TextStyle(fontWeight: FontWeight.bold),
                      ),
                    ],
                  ),
                  Expanded(
                    child: ListView.builder(
                      itemExtent: 40,
                      itemCount: controller.areaList.length,
                      itemBuilder: (ctx, index) {
                        final entity = controller.areaList[index];
                        return ListTile(
                          title: Text(
                            entity.name,
                            style: TextStyle(fontSize: 14),
                          ),
                          onTap: () {
                            if (entity.level == 0) {
                              controller.selectedCountry = entity;
                            } else if (entity.level == 1) {
                              controller.selectedProvince = entity;
                            } else if (entity.level == 2) {
                              controller.selectedCity = entity;
                              controller.addHistory(entity);
                              callback(entity);
                              Get.back();
                            }
                            controller.loadCountryList(entity.id);
                          },
                        );
                      },
                    ),
                  ),
                  const SizedBox(height: 30),
                ],
              ),
            );
          },
        );
      },
    );
  }

  static void showTranslationPrice(String? selectedPrice, void Function(DictionaryEntity entity) onFinished) {
    List<DictionaryEntity>? priceList = app.dictionaryEntityList('bus_translator_score');
    if (priceList == null) return;
    DictionaryEntity? selectedEntity;
    for (var e in priceList) {
      if (selectedPrice == e.dictValue) {
        e.isSelected = true;
        selectedEntity = e;
      } else {
        e.isSelected = false;
      }
    }
    showModalBottomSheet(
      context: Get.context!,
      builder: (ctx) {
        return Container(
          height: 0.5.sh,
          decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.only(
              topLeft: const Radius.circular(10.0),
              topRight: const Radius.circular(10.0),
            ),
          ),
          child: Column(
            children: [
              Padding(
                padding: const EdgeInsets.all(15.0),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Text('取消'.tr, style: TextStyle(color: Colors.white)),
                    Text(
                      '收费标准'.tr,
                      style: TextStyle(
                        fontSize: 16,
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                    InkWell(onTap: Get.back, child: Text('取消'.tr)),
                  ],
                ),
              ),
              Expanded(
                child: StatefulBuilder(
                  builder: (context, setState) {
                    return ListView.separated(
                      padding: const EdgeInsets.all(16),
                      itemCount: priceList.length,
                      separatorBuilder: (ctx, index) {
                        return SizedBox(height: 10,);
                      },
                      itemBuilder: (ctx, index) {
                        DictionaryEntity entity = priceList[index];
                        return InkWell(
                          onTap: () {
                            priceList.forEach((e) => e.isSelected = false);
                            entity.isSelected = true;
                            selectedEntity = entity;
                            setState((){});
                          },
                          child: Container(
                            padding: const EdgeInsets.symmetric(
                              horizontal: 16,
                              vertical: 8,
                            ),
                            decoration: BoxDecoration(
                              color: entity.isSelected
                                  ? AppColors.color_D7E9FF
                                  : AppColors.color_F7F7F7,
                              borderRadius: BorderRadius.circular(10),
                              border: Border.all(
                                color: entity.isSelected
                                    ? AppColors.color_278AFF
                                    : AppColors.color_F7F7F7,
                                width: 1,
                              ),
                            ),
                            child: Text(
                              entity.dictValue,
                              style: TextStyle(
                                fontSize: 14,
                                color: entity.isSelected
                                    ? AppColors.color_278AFF
                                    : AppColors.color_333333,
                              ),
                            ),
                          ),
                        );
                      },
                    );
                  }
                ),
              ),

              SafeArea(
                child: Container(
                  margin: EdgeInsets.symmetric(horizontal: 16, vertical: 16),
                  child: GLGradientButton(
                    onPressed: () {
                      if (selectedEntity != null) {
                        onFinished(selectedEntity!);
                        Get.back();
                      }

                    },
                    child: const Text(
                      '确定',
                      style: TextStyle(color: Colors.white),
                    ),
                  ),
                ),
              ),
            ],
          ),
        );
      },
    );

  }
}
